package com.travel.sight.scheduled;

import com.travel.sight.entity.AttractionInfoEntity;
import com.travel.sight.service.AttractionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 景点信息修改的定时任务
 */
@Slf4j
@Service
@EnableAsync //开启异步任务的支持，防止定时任务之间相互阻塞
@EnableScheduling //开启定时任务
public class AttractionInfoScheduled {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    AttractionInfoService attractionInfoService;


    /**
     * 每隔三分钟，同步redis和mysql中的点赞，收藏，评论数
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void updateLikeCollectionCommentNum(){
        log.info("redis和mysql中的点赞，收藏，评论数的同步...");
        //1. 取出所有的景点的点赞数，收藏数，评论数  map<attractionId,点赞数/收藏数/评论数>
        HashMap<Integer, Integer> likeNumMap = getValuesByPattern("*likeNum*");
        HashMap<Integer, Integer> collectionNumMap = getValuesByPattern("*collectionNum*");
        HashMap<Integer, Integer> commentNumMap = getValuesByPattern("*commentNum*");
        System.out.println(likeNumMap);
        System.out.println(collectionNumMap);
        System.out.println(commentNumMap);

        //2. 保存到数据库中
        List<AttractionInfoEntity> updateList = new ArrayList<>();

        List<AttractionInfoEntity> list = attractionInfoService.list();

        for (AttractionInfoEntity entity : list) {
            Integer attractionId = entity.getAttractionId();
            if(likeNumMap.containsKey(attractionId)){
                entity.setLikeNum(likeNumMap.get(attractionId));
            }
            if(collectionNumMap.containsKey(attractionId)){
                entity.setCollectionNum(collectionNumMap.get(attractionId));
            }
            if(commentNumMap.containsKey(attractionId)){
                entity.setCommentNum(commentNumMap.get(attractionId));
            }
            updateList.add(entity);
        }


        // 批量保存
        attractionInfoService.updateBatchById(updateList);

    }


    // 获取所有符合模式的键的值
    public HashMap<Integer,Integer> getValuesByPattern(String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);

        HashMap<Integer, Integer> map = new HashMap<>();
        if (keys != null) {
            for (String key : keys) {
                // 取出attractionId
                String numStr = key.substring(key.indexOf(":") + 1);
                int attractionId = Integer.parseInt(numStr);

                String value = redisTemplate.opsForValue().get(key);
                map.put(attractionId,Integer.parseInt(value));
            }
        }

        return map;
    }
}
